package com.jsgt.greekfire.ui.wallet

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import com.jsgt.greekfire.ui.dialog.InputDialogUtil
import kotlinx.android.synthetic.main.activity_identity_info.*

class IdentityInfoActivity : BaseActivity(), View.OnClickListener {

    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }
    private var identityBean: IdentityBean? = null

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_identity_info
    }

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        title = getString(R.string.identity_title)

        val id = intent.getIntExtra("id", -1)
        if (id < 0) {
            finish()
        }

        walletViewModel.getIdentity(id)?.observe(this, Observer {
            identityBean = it
            txvName.text = it.identityName
        })

        vName.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.vName -> {
                InputDialogUtil.showInputDialog(
                    this,
                    getString(R.string.identity_name_modify),
                    txvName.text.toString()
                ) {
                    if (identityBean != null && it.isNotEmpty()) {
                        val identityName = IdentityBean.Name(identityBean!!.id, it)
                        walletViewModel.updateIdentityName(identityName)
                    }
                }
            }
        }
    }
}